import java.util.List;
import java.util.Map;
import java.util.Set;

import org.dom4j.Element;

import com.alibaba.fastjson.JSONObject;
import com.justep.ui.wx.WXComponentContext;
import com.justep.ui.wx.component.WXComponent;

public class Wxtemplate extends WXComponent{
	
	public String getTagName(){
		return "view";
	}
	
	public JSONObject doExecute(Element bound, Map<String, Object> context, WXComponentContext wxContext) {
		JSONObject props = new JSONObject();	     
		
		String id = bound.attributeValue("id");
		String templateId = bound.attributeValue("templateId");
		bound.addAttribute("templateId","");
		// String url = bound.attributeValue("url");
		// bound.addAttribute("url","");
		// String params = bound.attributeValue("params");
		// bound.addAttribute("params","");

		String emphasisKeyword = bound.attributeValue("emphasisKeyword");
		bound.addAttribute("emphasisKeyword","");
		String keyword1 = bound.attributeValue("one");
		bound.addAttribute("one","");
		String keyword2 = bound.attributeValue("two");
		bound.addAttribute("two","");
		String keyword3 = bound.attributeValue("three");
		bound.addAttribute("three","");
		String keyword4 = bound.attributeValue("four");
		bound.addAttribute("four","");
		String keyword5 = bound.attributeValue("five");
		bound.addAttribute("five","");
		String keyword6 = bound.attributeValue("six");
		bound.addAttribute("six","");
		String keyword7 = bound.attributeValue("seven");
		bound.addAttribute("seven","");
		String keyword8 = bound.attributeValue("eight");
		bound.addAttribute("eight","");
		String keyword9 = bound.attributeValue("nine");
		bound.addAttribute("nine","");
		String keyword10 = bound.attributeValue("ten");
		bound.addAttribute("ten","");
		String toUser = bound.attributeValue("toUser");
		bound.addAttribute("toUser","");
		
		props.put("templateId", templateId);
		props.put("emphasisKeyword", emphasisKeyword);

		// props.put("url", url);
		// processExprValue(url,props,id,"url",bound,context,wxContext);
		props.put("toUser", toUser);
		processExprValue(toUser,props,id,"toUser",bound,context,wxContext);
		props.put("one", keyword1);
		processExprValue(keyword1,props,id,"one",bound,context,wxContext);
		props.put("two", keyword2);
		processExprValue(keyword2,props,id,"two",bound,context,wxContext);
		props.put("three", keyword3);
		processExprValue(keyword3,props,id,"three",bound,context,wxContext);
		props.put("four", keyword4);
		processExprValue(keyword4,props,id,"four",bound,context,wxContext);
		props.put("five", keyword5);
		processExprValue(keyword5,props,id,"five",bound,context,wxContext);
		props.put("six", keyword6);
		processExprValue(keyword6,props,id,"six",bound,context,wxContext);
		props.put("seven", keyword7);
		processExprValue(keyword7,props,id,"seven",bound,context,wxContext);
		props.put("eight", keyword8);
		processExprValue(keyword8,props,id,"eight",bound,context,wxContext);
		props.put("nine", keyword9);
		processExprValue(keyword9,props,id,"nine",bound,context,wxContext);
		props.put("ten", keyword10);
		processExprValue(keyword10,props,id,"ten",bound,context,wxContext);
		
		// props.put("params",params);
		// parsePageParams(params,props,id,bound,context,wxContext);

		return props;
	}
	
	protected void processExprValue(String expr, JSONObject props,String id,String name,Element bound, Map<String, Object> context,WXComponentContext wxContext){
		if((null==expr || "".equals(expr) )&& !"toUser".equals(name)) return;
		String paramsFn = "$exprBinding_" + id + "_" + name;
		props.put("$executeExprBinding" + "_" + name, paramsFn);
		Set<String> dataIds = getDataIds(bound, context);
		StringBuffer body = new StringBuffer("return " + expr);
		generateFn(paramsFn, body.toString(), wxContext, dataIds);
	}

	public void parsePageParams(String pageParams,JSONObject props,String id,Element bound, Map<String, Object> context,WXComponentContext wxContext) {
		if(pageParams != null && pageParams != "") {
			pageParams = pageParams.replaceAll("\"", "");
			pageParams = pageParams.substring(1, pageParams.length()-1);
			String[] arr = pageParams.split(",");
			for(String str : arr) {
				if(str.contains(":")) {
					String[] strs = str.split(":");
					processExprValue(strs[1].trim(), props, id, strs[0].trim(), bound, context, wxContext);
				}
			}
		}
	}

}